package itqf.controller;



import itqf.anno.ContentType;
import itqf.entity.Users;
import itqf.service.AdminServiceImpl;
import itqf.service.impl.AdminService;
import itqf.utils.SysConstant;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * ${PACKAGE_NAME}
 * 2020/9/18
 */
@WebServlet("/AdminController")
public class AdminController extends BaseController {
    private AdminService adminService = new AdminServiceImpl();
    public String adminLogin(HttpServletRequest request, HttpServletResponse response){
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        Users admin = adminService.findUser(username, password);
        if(admin!=null && admin.getUrole()==0){
            request.setAttribute("admin",admin);
            return SysConstant.FORWARD+SysConstant.FALG+"admin/admin.jsp";
        }else{
            return SysConstant.REDIRECT+SysConstant.FALG+"admin/login.jsp";
        }
    }
    public String changePage(HttpServletRequest request, HttpServletResponse response){
        int flag = Integer.parseInt(request.getParameter("flag"));
        request.getSession().setAttribute("status",flag);
        return SysConstant.FORWARD+SysConstant.FALG+"admin/userList.jsp";
    }
    @ContentType("application/json;charset=utf-8")
    public  List<Users> getUserList(HttpServletRequest request, HttpServletResponse response){
        int status =(int) request.getSession().getAttribute("status");
        List<Users> allUser = null;
        allUser = adminService.findUserByStatus(status);
        //request.getSession().removeAttribute("status");
        return allUser;
    }

    @ContentType("application/json;charset=utf-8")
    public  List<Users> searchUser(HttpServletRequest request, HttpServletResponse response){
        String username = request.getParameter("username");
        String gender = request.getParameter("gender");
        if(username==""&& gender==""){
            return getUserList(request,response);
        }else if(username!=""&&gender!=""){
            List<Users> allUser = adminService.searchUser(username,gender);
            List<Users> sexUser = new ArrayList<>();
            for (Users users : allUser) {
                if(users.getUsex().equals(gender)){
                    sexUser.add(users);
                }
            }
            return adminService.searchUser(username,gender);
        }else{
            return  adminService.searchUser(username,gender);

        }
    }
    @ContentType("application/json;charset=utf-8")
    public String  deleteUser(HttpServletRequest request, HttpServletResponse response){
        int id = Integer.parseInt(request.getParameter("id"));
        int count = adminService.delete(id);
        if(count > 0){
            return SysConstant.FORWARD+SysConstant.FALG+"AdminController?method=getUserList";
        }else{

            return SysConstant.FORWARD+SysConstant.FALG+"message.jsp";
        }
    }

}
